package com.aos.eap.activity;

import android.os.Handler;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aos.eap.R;
import com.aos.eap.base.BaseDialogActivity;
import com.aos.eap.base.Constant;
import com.aos.eap.bean.EqpInfoBean;
import com.aos.eap.bean.net.BaseResponseBean;
import com.aos.eap.utils.MyUtils;
import com.aos.eap.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import butterknife.BindView;

public class MFG_PeConfirmActivity extends BaseDialogActivity {


    @BindView(R.id.tvUser)
    TextView tvUser;
    @BindView(R.id.etEqpId)
    FreeUI_ClearEditText etEqpId;
    @BindView(R.id.tvLotId)
    TextView tvLotId;
    @BindView(R.id.tvDeviceId)
    TextView tvDeviceId;
    @BindView(R.id.tvPkg)
    TextView tvPkg;
    @BindView(R.id.etDesc)
    FreeUI_ClearEditText etDesc;
    @BindView(R.id.etRemark)
    FreeUI_ClearEditText etRemark;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    private EqpInfoBean eqpInfoBean;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_pe_confirm;
    }

    @Override
    protected void initView() {
        tvUser.setText(Constant.CURRENT_USER);
        MyUtils.setEditTextListener(etEqpId, this, this::requestInfoByEqpId);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("提报PE确认");
        freeUI_titleView.setLeftListener(view -> showBackDialog(MFG_PeConfirmActivity.this));
        freeUI_titleView.setRightListener("提交", v -> {
            if (MyUtils.isFastClick()){
                showToast("您点击的太快了",false);
                return;
            }
            if (eqpInfoBean == null) {
                showToast("未获取到页面信息，请重新扫描设备编号", false);
                etEqpId.setText("");
                MyUtils.setEditTextFocus(etEqpId);
                return;
            }
            String desc = etDesc.getText().toString().trim();
            if (FreeApi_StringUtils.isEmpty(desc)) {
                showToast("请输入故障描述", false);
                MyUtils.setEditTextFocus(etDesc);
                return;
            }
            JsonObject map = new JsonObject();
            map.addProperty("EqpID", eqpInfoBean.getEqpID());
            map.addProperty("LotID", eqpInfoBean.getLotID());
            map.addProperty("DeviceID", eqpInfoBean.getDeviceID());
            map.addProperty("PKG", eqpInfoBean.getPKG());
            map.addProperty("FaultDesc", desc);
            map.addProperty("Remark", etRemark.getText().toString().trim());
            requestSubmit(map);
        });
        return new FreeUI_AddViewEntity("header", llMain);
    }

    private void requestSubmit(JsonObject map) {
        NetUtils.requestGetNet(this, "EMS_PEComfirmCall", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<?>>(
                new TypeToken<BaseResponseBean<?>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<?>> response) {
                if (response.body().isSuccess(getApplicationContext(), false,getSupportFragmentManager())) {
                    showToast("提交成功", true);
                    new Handler().postDelayed(MFG_PeConfirmActivity.this::finish, 2000);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<?>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_PEComfirmCall");
            }
        });
    }

    private void requestInfoByEqpId(String eqpId) {
        JsonObject map = new JsonObject();
        map.addProperty("EqpID", eqpId);
        NetUtils.requestGetNet(this, "EMS_PEComfirmCallLoad", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<EqpInfoBean>>(
                new TypeToken<BaseResponseBean<EqpInfoBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<EqpInfoBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true,getSupportFragmentManager())) {
                    eqpInfoBean = response.body().getResponse().getBody();
                    setPageData(eqpInfoBean);
                }else {
                    eqpInfoBean = null;
                    setPageData(new EqpInfoBean());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<EqpInfoBean>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_PEComfirmCallLoad");
                eqpInfoBean = null;
                setPageData(new EqpInfoBean());
            }
        });
    }

    private void setPageData(EqpInfoBean eqpInfoBean) {
        tvLotId.setText(eqpInfoBean.getLotID());
        tvDeviceId.setText(eqpInfoBean.getDeviceID());
        tvPkg.setText(eqpInfoBean.getPKG());
    }
}
